﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;
using Gadgeteer.Modules.Seeed;

namespace Gadgeteer.Modules.Community.XBee.Samples.Spider
{
    /// <summary>
    /// NOTE: THIS PROJECT IS NOT IMPLEMENTED. It depends on remote test execution feature. This feature involves a new message type 'test', which
    /// will describe functional tests to run, and the test summary will be reported back to the PC client. The tests will be run from the PC client.
    /// For now you can use this project to ad hoc testing your Spider/Hydra/Cerberus etc.
    /// This program is run on a GHI Spider. The XBeeClient is not added using the designer, but declaratively using code
    /// </summary>
    public partial class Program
    {
        XBeeClient client1;

        void ProgramStarted()
        {

            Debug.Print("Program Started");

            // NOTE: you must connect the XBee module to socket 4, OR update the following line with the socket you 
            // have connected the XBee module to. The GHI XBee module can be connected to socket types 'K' and 'U'.
            // On The Spider, the valid sockets are 4, 8, and 11
            client1 = new XBeeClient(4);

            // you must register event handlers for any events you are interested in

            client1.TransmitStatusReceived += new TransmitStatusReceivedEventHandler(client1_TransmitStatusReceived);
            client1.TransmitReceive+=new TransmitReceiveEventHandler(client1_TransmitReceive);
            client1.DataReceived += new DataReceivedEventHandler(client1_DataReceived);
            client1.StringReceived += new StringReceivedEventHandler(client1_StringReceived);
            client1.ATResponseReceived+=new ATResponseReceivedEventHandler(client1_ATResponseReceived);

            client1.SendLocalATCommandAsync("MY");
            client1.SendLocalATCommandAsync("SH");
            client1.SendLocalATCommandAsync("SL");
        }

        void client1_StringReceived(object sender, StringReceivedEventArgs e)
        {
            Debug.Print("String Received: " + e.Message);
        }

        void client1_DataReceived(object sender, DataReceivedEventArgs e)
        {
            Debug.Print("Data Received: " + e.Data.Length);
        }

        void client1_TransmitStatusReceived(object sender, TransmitStatusReceivedEventArgs e)
        {
            Debug.Print("Transmit Success: " + e.Result.DeliveryStatus);
        }

        void client1_TransmitReceive(object sender, TransmitReceiveEventArgs e)
        {
            Debug.Print(e.Result.ToString());
        }

        void client1_ATResponseReceived(object sender, ATResponseReceivedEventArgs e)
        {
            Debug.Print(e.Result.ToString());
        }

        void client1_DiscoverNodesCompleted(object sender, DiscoverNodesCompletedEventArgs e)
        {
            Debug.Print("Discover nodes success");
        }
    }
}
